let request_url = "";
const mockOpenid = "";
const env = "dev";
// const env = "prd";
const whiteList = [
	'/crm/clue/getKzCount'
]

import pageConfig from '@/config'
request_url = pageConfig.baseUrl;

export const GET = (url, data) => {
	return new Promise((resolve, reject) => {
		//过滤计数的加载框
		if (!whiteList.includes('/crm/clue/getKzCount')) {
			uni.showLoading({
				title: '加载中',
				mask: true
			})
		}
		uni.request({
			url: url.includes('http') ? url : request_url + url,
			method: 'GET',
			header: {
				'content-type': 'application/json',
				'Authorization': uni.getStorageSync('token') ?
					`Bearer ${uni.getStorageSync('token')}` : ''
			},
			data: data,
			success: res => {
				uni.hideLoading()
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					uni.showToast({
						icon: 'none',
						title: '登录态已过期',
						duration: 2000
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/index'
						})
					}, 2000)
				} else if (res.data.code == 500) {
					uni.showToast({
						icon: 'none',
						title: res.msg || '服务器错误',
						duration: 2000
					})
				}
			}
		})
	})
}

export const POST = (url, data) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中',
			mask: true
		})
		uni.request({
			url: url.includes('http') ? url : request_url + url,
			method: 'POST',
			header: {
				'content-type': 'application/json',
				'Authorization': uni.getStorageSync('token') ?
					`Bearer ${uni.getStorageSync('token')}` : ''
			},
			data: data,
			success: res => {
				uni.hideLoading()
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					uni.showToast({
						icon: 'none',
						title: '登录态已过期',
						duration: 2000
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/index'
						})
					}, 2000)
				} else if (res.data.code == 500) {
					uni.showToast({
						icon: 'none',
						title: res.msg || '服务器错误',
						duration: 2000
					})
				}
			}
		})
	})
}

export const PUT = (url, data) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中',
			mask: true
		})
		uni.request({
			url: url.includes('http') ? url : request_url + url,
			method: 'PUT',
			header: {
				'content-type': 'application/json',
				'Authorization': uni.getStorageSync('token') ?
					`Bearer ${uni.getStorageSync('token')}` : ''
			},
			data: data,
			success: res => {
				uni.hideLoading()
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					uni.showToast({
						icon: 'none',
						title: '登录态已过期',
						duration: 2000
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/index'
						})
					}, 2000)
				} else if (res.data.code == 500) {
					uni.showToast({
						icon: 'none',
						title: res.msg || '服务器错误',
						duration: 2000
					})
				}
			}
		})
	})
}
export const DELETE = (url, data) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			mask: true
		})
		uni.request({
			url: url.includes('http') ? url : request_url + url,
			method: 'DELETE',
			header: {
				'content-type': 'application/json',
				'Authorization': uni.getStorageSync('token') ?
					`Bearer ${uni.getStorageSync('token')}` : ''
			},
			data: data,
			success: res => {
				console.log('返回response', res)
				uni.hideLoading()
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 401) {
					uni.showToast({
						icon: 'none',
						title: '登录态已过期',
						duration: 2000
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/index'
						})
					}, 2000)
				} else if (res.data.code == 500) {
					uni.showToast({
						icon: 'none',
						title: res.msg || '服务器错误',
						duration: 2000
					})
				}
			}
		})
	})
}